﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;
using RestaurantNS = BuXiaChu.OrderDishes.Web.Admin.Modules.Restaurant;
namespace BuXiaChu.OrderDishes.Web.Admin.Restaurant
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Dishes1 : HttpHandlerBase, IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                case "1":
                    GetList();
                    break;
                case "4":
                    Delete();
                    break;
                default: break;
            }
        }

        private void GetList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                var pageCount = 0;
                var pageIndex = 0;
                var pageSize = 0;
                GetPageBarParams(out pageIndex, out pageSize);
                var sqlWhere = GetSearchCondition();
                UserControlPreviewManager<RestaurantNS.DishesList> preivewManager = new UserControlPreviewManager<RestaurantNS.DishesList>();
                RestaurantNS.DishesList dishesList = preivewManager.LoadUserControl("~/Modules/Restaurant/DishesList.ascx");
                dishesList.PageDataList = RestaurantManager.SearchDishes(sqlWhere, pageIndex, pageSize, out pageCount);
                dishesList.PageIndex = pageIndex;
                dishesList.PageCount = pageCount;
                result.message = preivewManager.RenderHtml(dishesList);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        private void Delete()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var id = GetFormValue("id");
            var iId = 0;
            if (int.TryParse(id, out iId))
            {
                result.success = RestaurantManager.DeleteDishesById(iId);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private string GetSearchCondition()
        {
            var sqlWhere = " restaurantId=" + GetFormValue("restaurantId");
            var name = GetFormValue("name");
            if (string.IsNullOrEmpty(name) == false)
            {
                sqlWhere += " and name like'%" + name.Trim() + "%'";
            }
            return sqlWhere;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
